<template>
  <el-dialog title="分配角色" :visible.sync="roleShow" @close="closeEvent">
    <h2>{{ roleIds }}</h2>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submmit">提交</el-button>
        <el-button>取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRolesPut } from '@/api/employees'
export default {
  data() {
    return {
      roleShow: false,
      roleIds: [],
      list: [],
      //   用户id
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysRole()
      this.list = res.data.rows
    },
    //   角色数据回显
    async getUserRole(id) {
      this.id = id
      const res = await sysUser(id)
      this.roleIds = res.data.roleIds
    },
    // 提交修改
    async submmit() {
      await sysUserAssignRolesPut({
        id: this.id,
        roleIds: this.roleIds
      })
      this.$message.success('修改成功')
      //   关闭弹框
      this.roleShow = false
    },
    closeEvent() {
      // 清空已勾选数据
      this.roleIds = []
    }
  }
}
</script>

<style></style>
